package com.c1g19.dao.guide;

import com.c1g19.dao.BaseDao;
import com.c1g19.utils.BasicRowProcessorFix;
import com.c1g19.utils.DbControl;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

/**
 * 涉及3D导览的Dao层
 * Created by Mr_C on 2017/3/7.
 */
public class GuideDao extends BaseDao {
    private static final Logger logger = Logger.getLogger(GuideDao.class);

    public Map<String, Object> getNearOffice(int picture_id, float x, float y) {
        logger.info("getNearOffice");
        Map<String, Object> userMap = null;
        Connection conn = null;
        try {
            conn = DbControl.getConnection();
            //这里需要改成among区间或者在页面相关出生成一个图标供点击
            String sql = "select * from office_on_picture where picture_id=? and x=? and y = ? and display = 1;";
            userMap = getQueryRunner().query(conn, sql,
                    new MapHandler(new BasicRowProcessorFix()), picture_id, x, y);
            logger.info("user:" + userMap);
        } catch (Exception e) {
            logger.error("", e);
        } finally {
            DbUtils.closeQuietly(conn);
        }
        return userMap;
    }

    /**
     * 得到所有的主导览图
     * @return 所有主病例类的MapList
     */
    public Map<String, Object> getAllMainPicture(){
        logger.info("执行 getAllMainPicture");
        Map<String, Object> dataMap = new HashMap<String, Object>();
        List<Map<String, Object>> allMainPictureList = new ArrayList<Map<String, Object>>(); //主导览图的list
        long totalCount = 0; // 主导览图总数
        Connection conn = null;
        String sql = "";
        try {
            conn = DbControl.getConnection();

            sql = "select count(main_picture_id) from main_picture where display>0";
            totalCount = getQueryRunner().query(conn, sql,
                    new ScalarHandler<Long>());
            sql = "select * from main_picture where display>0;";
            allMainPictureList = getQueryRunner().query(conn, sql,
                    new MapListHandler(new BasicRowProcessorFix()));
            logger.info("查询到的总导览图共有" + allMainPictureList.size() + "张");
        } catch (Exception e) {
            logger.error("", e);
        } finally {
            DbUtils.closeQuietly(conn);
        }
        dataMap.put("totalCount", totalCount);
        dataMap.put("dataList", allMainPictureList);
        return dataMap;
    }

    /**
     * 通过图片ID搜索这张图上的科室信息
     * @param picture_id
     * @return 该图上所有可见科室的坐标点
     */
    public Map<String, Object> getOfficeByPId(int picture_id){
        logger.info("getOfficeByPId");
        Map<String, Object> dataMap = new HashMap<String, Object>();
        String sql = "";
        long totalCount = 0;
        //存放该图上科室坐标的队列
        List<Map<String, Object>> officelocMapList = new ArrayList<Map<String, Object>>();
        Connection conn = null;
        try {
            conn = DbControl.getConnection();

            sql = "select count(office_on_picture_id) from office_on_picture where display>0";
            totalCount = getQueryRunner().query(conn, sql,
                    new ScalarHandler<Long>());

            logger.info("picture_id: "+picture_id);
            sql = "select * from office_on_picture where picture_id=? and display > 0;";
            officelocMapList = getQueryRunner().query(conn, sql,
                    new MapListHandler(new BasicRowProcessorFix()), picture_id);
            logger.info("找到了该图上的"+officelocMapList.size()+"个诊室");
        } catch (Exception e) {
            logger.error("", e);
        } finally {
            DbUtils.closeQuietly(conn);
        }
        dataMap.put("totalCount", totalCount);
        dataMap.put("dataList", officelocMapList);
        return dataMap;
    }
}
